#include "./KeyBoardShortcuts.h"

#include <mutex>

namespace qbe::input {

std::size_t KeyBoardShortcuts::Hash::operator()(const KeyBoardShortcuts& self) const {
  std::size_t h1 = std::hash<vsg::KeySymbol>()(self.keyBase);
  std::size_t h2 = std::hash<vsg::KeyModifier>()(self.keyModifier);

  std::size_t result = (h1 << 16) | (h2 & 0xFFFF);

  return result;
}

std::mutex key_map_mutex;

bool KeyBoardShortcutsManager::add(KeyBoardShortcuts keyBoard, std::string name) {
  std::unique_lock<std::mutex>(key_map_mutex);
  if (key_map.find(keyBoard) != key_map.end()) {
    return false;
  }

  key_map[keyBoard] = name;
  return true;
}
bool KeyBoardShortcutsManager::change(KeyBoardShortcuts keyBoard, std::string name) {
  std::unique_lock<std::mutex>(key_map_mutex);
  if (key_map.find(keyBoard) != key_map.end()) {
    key_map[keyBoard] = name;
    return true;
  }
  return false;
}
bool KeyBoardShortcutsManager::remove(KeyBoardShortcuts keyBoard) {
  std::unique_lock<std::mutex>(key_map_mutex);
  if (key_map.find(keyBoard) != key_map.end()) {
    key_map.erase(keyBoard);
    return true;
  }
  return false;
}

const std::unordered_map<KeyBoardShortcuts, std::string, KeyBoardShortcuts::Hash> KeyBoardShortcutsManager::getMap() const {
  return key_map;
}
}  // namespace qbe::input